# -*- coding: utf-8 -*-
#!/usr/bin/python

##copyright 2014 Sebastian Stetter
##This file is part of the Lemonade Small Business Suite project and released under GPL v2
##http://lemonade.sebastianstetter.de

"""
******************************************************************************
* Lemonade Dial for Android Phones
* This python script is written for SL4A on Android devices
* for more info on SL4A see: https://code.google.com/p/android-scripting/
******************************************************************************
"""

from SocketServer import ThreadingTCPServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
from SimpleXMLRPCServer import SimpleXMLRPCDispatcher

import sys, socket,  struct
import android

__version__="0.0.3"

droid=android.Android()

class SimpleThreadingXMLRPCServer(ThreadingTCPServer, SimpleXMLRPCDispatcher):
    def __init__(self, addr, requestHandler = SimpleXMLRPCRequestHandler, logRequests = 1):
        self.logRequests = logRequests
        if sys.version_info[:2] < (2, 5):
            SimpleXMLRPCDispatcher.__init__(self)
        else:
            SimpleXMLRPCDispatcher.__init__(self, allow_none = False, encoding = None)
        ThreadingTCPServer.__init__(self, addr, requestHandler)

class XmlrpcHandler:
    def dial_number(self, p, number_to_dial,  name=''):
        if password == p:
            droid.vibrate()
            droid.makeToast("Lemonade: Dialing number %s for calling %s"%(number_to_dial, name))
            droid.phoneDialNumber(str(number_to_dial))
            return [0,"OK"]
        else: 
            return [2,"authentication failed"]
            
    def call_number(self, p, number_to_dial, name=''):
        if password == p:
            droid.vibrate()
            droid.makeToast("Lemonade: Calling %s on number %s"%(name, number_to_dial))
            droid.phoneCallNumber(str(number_to_dial))
            return [0,"OK"]
        else: 
            return [2,"authentication failed"]

class LemonadeDialer():
    def __init__(self, port,  password,  ssid):
        self.port = port
        self.password = password
        self.ssid = ssid
        self.server = None
        
    def get_IP(self):
        if droid.checkWifiState():
            ipdec = droid.wifiGetConnectionInfo().result['ip_address']
            ip=socket.inet_ntoa(struct.pack('L', ipdec))
            droid.makeToast(ip)
            return ip
        else:
           return None
        
    def getSSID(self):
        if droid.checkWifiState():
            current_SSID = droid.wifiGetConnectionInfo().result['ssid']
            droid.makeToast(current_SSID)
            print current_SSID
            return current_SSID
        else:
            return None
        
    def start_server(self):
        
        if self.getSSID()==self.ssid:
            self.server = SimpleThreadingXMLRPCServer((self.get_IP(), self.port))
            self.server.register_instance(XmlrpcHandler())
            droid.makeToast("XMLRPC-server listening on http://%s:%i."%(self.get_IP(), self.port))
            print("XMLRPC-server listening on http://%s:%i."%(self.get_IP(), self.port))
            self.server.serve_forever()
            return True
        else:
            droid.makeToast("Wireless network %s not available"%self.ssid)
            return False
    
if __name__ == '__main__':
    #TODO: implement storage of these values in settings and create user interface for this APP
    password="secret"
    port = 50505
    SSID='JUPITER'
    lemonade=LemonadeDialer(port, password, SSID)
    lemonade.start_server()
    
